var Email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
var Phone = /^(1(3|5|8)\d{9})|(147\d{8})$/;
function $() {
	 var results = [], element;
	 for (var i = 0; i < arguments.length; i++) {
		 element = arguments[i];
		 if (typeof element == 'string')
			 element = document.getElementById(element);
		 results.push(Element.extend(element));
	 }
	 return results.reduce();
}

function $$(id) {
	return $(id).value;
}

function tbSwitch(panel1, panel2) {
	var theDisp = 'table';
	var theFocusEle = document.getElementById('queryFocus');
	if (navigator.appName == 'Microsoft Internet Explorer') {
		theDisp = 'block';
	}
	if (document.getElementById(panel1).style.display == 'none') {
		document.getElementById(panel1).style.display = theDisp;
		document.getElementById(panel2).style.display = 'none';
	} else {
		document.getElementById(panel1).style.display = 'none';
		document.getElementById(panel2).style.display = theDisp;
		if (theFocusEle) {
			theFocusEle.focus();
		}
	}
	document.getElementById("tbSuccessId").style.display = 'none';
}

function oPop(theTD) {
	var contentDiv = theTD.childNodes[1];
	contentDiv.style.display = "block";
}

function oPopHide(theTD) {
	var contentDiv = theTD.childNodes[1];
	contentDiv.style.display = "none";
}

//根据id检查字段不能为空，且不能超长
function $sbl(id, name, length){
	if(isBlank($$(id))){
		alert("'" + name + "'不能为空");
		$(id).focus();
		return false;
	}
	if(id=="email"){
		if(!Email.test($$(id))){
			alert("'"+name+"'格式不正确");
			$(id).focus();
			return false;
		}
	}
	if(id=="mobile"){
		if(!Phone.test($$(id))){
			alert("'"+name+"'格式不正确");
			$(id).focus();
			return false;
		}
	}
	if(isTooLong($$(id), length)){
		alert("'" + name + "'的长度不能大于" + length);
		$(id).focus();
		return false;
	}
	return true;
}

//根据id检查字段不能为空
function $sb(id, name){
	if(isBlank($$(id))){
		alert("'" + name + "'不能为空");
		$(id).focus();
		return false;
	}
	return true;
}

//根据id检查字段不能超长
function $sl(id, name, length) {
	if(isTooLong($$(id), length)){
		alert("'" + name + "'的长度不能大于" + length);
		$(id).focus();
		return false;
	}
	return true;
}
//根据id检查日期类型字段不能空且格式正确
function $dbf(id, name){
	if(isBlank($$(id))){
		alert("'" + name + "'不能为空");
		$(id).focus();
		return false;
	}
	if(!dateCheck($$(id))){
		alert("'" + name + "'为日期类型，格式不正确");
		$(id).focus();
		return false;
	}
	return true;
}
//根据id检查日期类型字段格式正确
function $df(id, name){
	if($$(id).length == 0){
		return true;
	}
	if(!dateCheck($$(id))){
		alert("'" + name + "'为日期类型，格式不正确");
		$(id).focus();
		return false;
	}
	return true;
}

//根据id检查数字类型字段格式正确
function $ff(id, name){
	if($$(id).length == 0){
		return true;
	}
	if(!isFloat($$(id))){
		alert("'" + name + "'为浮点数类型，格式不正确");
		$(id).focus();
		return false;
	}
	return true;
}

//check if the string is blank
function isBlank(str) {
	var returnString = str;
	while (returnString.charAt(0) == " " || returnString.charAt(0) == "\r"
			|| returnString.charAt(0) == "\t" || returnString.charAt(0) == "\n")
		returnString = returnString.substring(1, returnString.length);
	while (returnString.charAt(returnString.length - 1) == " "
			|| returnString.charAt(returnString.length - 1) == "\r"
			|| returnString.charAt(returnString.length - 1) == "\t"
			|| returnString.charAt(returnString.length - 1) == "\n")
		returnString = returnString.substring(0, returnString.length - 1);
	return returnString.length <= 0;
}

//check string length
function isTooLong(str, length) {
	if(str.length > length){
		return true;
	} else {
		return false;
	}
}

//check if input is int
function isInt(str) {
	var tem = str + "";
	var intPattern = new RegExp("^\\d+$", "g");
	return tem.match(intPattern) == tem;
}

//check if input is float
function isFloat(str) {
	var tem = str + "";
	var floatPattern = new RegExp("^\\d+(\\.\\d+)?([E|e][+|-]?\\d+)?$", "g");
	return tem.match(floatPattern) == tem;
}

//year range variables
var yMin = 1900;
var yMax = 2200;
// check date format
function dateCheck(datevalue) {
	var s = datevalue.split("-");
	if (s.length != 3) {
		return false;
	} else {
		if (s[0].length != 4) {
			return false;
		}
		if (s[1].length != 2) {
			return false;
		}
		if (s[2].length != 2) {
			return false;
		}
		var y = parseInt(s[0], 10);
		var m = parseInt(s[1], 10);
		var d = parseInt(s[2], 10);
		if (isNaN(y) || isNaN(m) || isNaN(d)) {
			return false;
		} else {
			if (y <= yMax && y >= yMin) {
				if (m < 1 || m > 12) {
					return false;
				} else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8
						|| m == 10 || m == 12) {
					if (d > 31 || d < 1) {
						return false;
					}
				} else if (m == 2) {
					var feb = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0) ? 29
							: 28;
					if (d > feb || d < 1) {
						return false;
					}
				} else if (d > 30 || d < 1) {
					return false;
				}
			} else {
				return false;
			}
		}
	}
	return true;
}